<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use App\Models\RevenueStatement;
use Modules\Manager\Entities\Department;

class CreateRevenueDate implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $order;
    public function __construct()
    {
        //
       // $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $year = date('Y'); // 获取当前年份，发布正式服的时候手动添加下2024年的
        // 当前年份是否有记录，如果没有记录，就生成今年的记录
        $yearlist = RevenueStatement::query()->where('year',$year)->get();
        if(count($yearlist)==0){
            $departmentlist = Department::query()->get();
            $data = [];
            for ($j=0; $j < count($departmentlist); $j++) {
                for ($i=1; $i <= 12; $i++) {
                    $data[]= [
                        'department_id'=> $departmentlist[$j]->id,
                        'department_name'=> $departmentlist[$j]->name,
                        'year'=> $year,
                        'month'=> $i
                    ];
                }
            }
            if(count($data)>0){
                DB::table('revenue_statements')->insert($data);
            }
        }
    }
}
